package com.eclipse.e0112_多线程.同步锁;
/*
 * 没有同步锁 会出现奇数
 */
public class 同步锁_偶数生成器 {
	public static void main(String[] args) {
		Even e = new Even();
		
		Task1 t1 = new Task1(e);
		Thread tt1 = new Thread(t1);
		tt1.start();

		Task2 t2 = new Task2(e);
		Thread tt2 = new Thread(t2);
		tt2.start();

	}

}

class Task1 implements Runnable {
	
	private Even e;
	Task1(Even e){
		this.e = e;
	}
	public  void run() {
		synchronized(e) {
			while(true) {
				e.add();
//				if(e.getNum()%2 !=0) {
					System.out.println(e.getNum());
//				}
			}
		}
		
		
	}
}

class Task2 implements Runnable {
	private Even e;
	Task2(Even e){
		this.e = e;
	}
	public  void run() {
		synchronized(e) {
			while(true) {
				e.add();
//				if(e.getNum()%2 !=0) {
					System.out.println(e.getNum());
//				}
				
			}
		}
		
	}
}

class Even {
	private int num;

	void add() {
		num++;
		num++;
	}

	int getNum() {
		return this.num;
	}
}